package Year22_Month06.day17;

import java.util.LinkedList;
import java.util.Queue;

public class 找树左下角的值_迭代写法 {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int num = 0;//实时更新每一层最左边的节点值
        while(!queue.isEmpty()){
            int size = queue.size();
            int key = size;
            while(size > 0){
                TreeNode node = queue.poll();
                //如果size == key，证明是当前层次的第一个遍历元素，就要更新值
                if(size == key) num = node.val;
                if(node.left != null) queue.offer(node.left);
                if(node.right != null) queue.offer(node.right);
                size--;
            }
        }
        return num;
    }
}
